package com.mttqq.family.core;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.mttqq.family.action.BaseAction;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.Result;
/*******************************************************************************
 * <b>类名:JsonResult.java</b> <br/>
 * 功能：json web 返回对像<br/>
 * 日期: 2011-06-05 <br/>
 * 
 * @author 谢广泉 xiegqooo@hotmail.com
 * @version 1.0.0
 * 
 ******************************************************************************/
public class JsonResult implements Result {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	public void execute(ActionInvocation invocation) throws Exception {
		 //定义上下文的环境  
        HttpServletRequest request = ServletActionContext.getRequest();  
        HttpServletResponse response = ServletActionContext.getResponse(); 
        
        // 设置文件MIME
        response.setCharacterEncoding(request.getCharacterEncoding());  
//        response.setCharacterEncoding("UTF-8");  
        //这里ContentType 需是application/json JS否则会出错。 
        response.setContentType("text/html;charset=UTF-8");  
        
        // 输出内容 
        PrintWriter out = response.getWriter();
        
        // 读取 action 对象的 json 对像
        BaseAction ba = (BaseAction)invocation.getAction();
        
        // 输出对象
        out.print(ba.jsonresult);
	}

}
